package cn.sc.summer.mybatis.util;

import cn.sc.summer.mybatis.properties.DataSourceProperties;
import com.alibaba.druid.pool.DruidDataSource;
import cn.sc.summer.constant.mybatisplus.datasource.DataSourceConstant;
import cn.sc.summer.constant.mybatisplus.SqlConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.jta.atomikos.AtomikosDataSourceBean;
import org.springframework.stereotype.Component;

import javax.sql.XADataSource;
import java.sql.SQLException;

/**
 * 类名：数据源创建工具类
 *
 * @author a-xin
 * @date 2024/7/30 16:52
 */
@Slf4j
@Component
public class DataSourceUtil {

    /**
     * 跨库事务管理构建
     *
     * @param xaDataSource   数据源
     * @param dataSourceType 数据源类型
     * @return 数据源管理器
     */
    public static AtomikosDataSourceBean atomikosDataSourceBuild(XADataSource xaDataSource, String dataSourceType) {
        AtomikosDataSourceBean atomikosDataSourceBean = new AtomikosDataSourceBean();
        atomikosDataSourceBean.setXaDataSourceClassName(DataSourceConstant.DATA_SOURCE_DRUID_XA);
        atomikosDataSourceBean.setXaDataSource(xaDataSource);
        atomikosDataSourceBean.setUniqueResourceName(dataSourceType);
        atomikosDataSourceBean.setMinPoolSize(0);
        atomikosDataSourceBean.setMaxPoolSize(20);
        atomikosDataSourceBean.setBorrowConnectionTimeout(60);
        atomikosDataSourceBean.setTestQuery(SqlConstant.SELECT_ONE);
        return atomikosDataSourceBean;
    }

    /**
     * 创建默认druid数据源信息
     *
     * @param properties      数据源参数
     * @param druidDataSource 数据源
     * @param <T>             数据源类型
     * @return 数据源
     */
    public static <T extends DruidDataSource> T createDruidDataSource(DataSourceProperties properties, T druidDataSource) {
        druidDataSource.setDriverClassName(properties.getDriverClassName());
        druidDataSource.setUrl(properties.getUrl());
        druidDataSource.setUsername(properties.getUsername());
        druidDataSource.setPassword(properties.getPassword());

        druidDataSource.setInitialSize(properties.getInitialSize());
        druidDataSource.setMaxActive(properties.getMaxActive());
        druidDataSource.setMinIdle(properties.getMinIdle());
        druidDataSource.setMaxWait(properties.getMaxWait());
        druidDataSource.setTimeBetweenEvictionRunsMillis(properties.getTimeBetweenEvictionRunsMillis());
        druidDataSource.setMinEvictableIdleTimeMillis(properties.getMinEvictableIdleTimeMillis());
        druidDataSource.setMaxEvictableIdleTimeMillis(properties.getMaxEvictableIdleTimeMillis());
        druidDataSource.setValidationQuery(properties.getValidationQuery());
        druidDataSource.setValidationQueryTimeout(properties.getValidationQueryTimeout());
        druidDataSource.setTestOnBorrow(properties.isTestOnBorrow());
        druidDataSource.setTestOnReturn(properties.isTestOnReturn());
        druidDataSource.setTestWhileIdle(properties.isTestWhileIdle());
        druidDataSource.setPoolPreparedStatements(properties.isPoolPreparedStatements());
        druidDataSource.setMaxOpenPreparedStatements(properties.getMaxOpenPreparedStatements());
        druidDataSource.setSharePreparedStatements(properties.isSharePreparedStatements());
        druidDataSource.setKeepAlive(properties.isKeepAlive());

        try {
            druidDataSource.setFilters(properties.getFilters());
            druidDataSource.init();
        } catch (SQLException e) {
            log.error("==> DynamicDataSourceFactory is error: {}", e.getMessage());
        }
        return druidDataSource;
    }

}
